//
//  DataViewController.m
//  jizhiyunduan
//
//  Created by 鞠超 on 16/4/13.
//  Copyright © 2016年 JC. All rights reserved.
//

#import "DataViewController.h"

#import "XiaofeiViewController.h"
#import "FangwenViewController.h"
#import "PaimingViewController.h"

@interface DataViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

//需要赋值的数据
//1
@property (nonnull, strong) UILabel * consume;
@property (nonnull, strong) UILabel * click;
@property (nonnull, strong) UILabel * balance;
//2
@property (nonnull, strong) UILabel * baidu_pc;
@property (nonnull, strong) UILabel * baidu_phone;
@property (nonnull, strong) UILabel * interlinkage;
//3
@property (nonnull, strong) UILabel * keywords_sum;
@property (nonnull, strong) UILabel * page_1;
@property (nonnull, strong) UILabel * page_2;
@property (nonnull, strong) UILabel * page_3;



@end

@implementation DataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self p_loginMoren];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

#pragma mark - navi
- (void)p_navi
{
    _lblTitle.text = [NSString stringWithFormat:@"数据分析"];
    _lblTitle.font = [UIFont systemFontOfSize:20];
    
    
    [self addRightButton:@"shuaxin@2x.png"];
//    _imgRight.frame = CGRectMake(SCREEN_WIDTH - 5 - 30, _imgRight.frame.origin.y - 1.5, _imgRight.frame.size.width, _imgRight.frame.size.height);
//    _btnRight.frame = CGRectMake(SCREEN_WIDTH - 10 - 40, _lblRight.frame.origin.y + 0, 40, _lblRight.frame.size.height - 10);
    
    _btnLeft.hidden = YES;
}

//刷新
- (void)clickRightButton:(UIButton *)sender
{    
//    NSLog(@"重新加载一遍数据,给动画");
    [SVProgressHUD setMinimumDismissTimeInterval:1.0f];
    
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    
    [SVProgressHUD setBackgroundColor:navi_bar_bg_color];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    
    [SVProgressHUD showSuccessWithStatus:@"刷新成功"];
}

//显示tabbar
-(void)viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] showTabBar];
}

#pragma mark - 布局
- (void)p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self.view addSubview:self.tableView];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        return 100;
    }
    else
    {
        return 140;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    
    CGFloat length_x = (SCREEN_WIDTH - 10) / 4;
    
    switch (indexPath.row)
    {
        case 0:
        {
            //1
            UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(5 + length_x / 2 - 27.5, 50 - 27.5, 55, 55)];
            image.image = [UIImage imageNamed:@"xiaofei@2x"];
            [cell addSubview:image];
            
            //2
            self.consume = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x, 30, length_x, 25)];
            self.consume.text = @"￥127.00";
            self.consume.font = [UIFont systemFontOfSize:15];
//            self.consume.backgroundColor = [UIColor orangeColor];
            self.consume.textAlignment = NSTextAlignmentCenter;
            [cell addSubview:self.consume];
            
            UILabel * label_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x, CGRectGetMaxY(self.consume.frame) + 5, length_x, 20)];
            label_1.text = @"消费总额";
            label_1.font = [UIFont systemFontOfSize:14];
            label_1.textAlignment = NSTextAlignmentCenter;
            label_1.textColor = [UIColor grayColor];
            [cell addSubview:label_1];
            
            
            //3
            self.click = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 2, 30, length_x, 25)];
            self.click.text = @"￥33.00";
            self.click.font = [UIFont systemFontOfSize:15];
            self.click.textColor = [UIColor redColor];
            self.click.textAlignment = NSTextAlignmentCenter;
            [cell addSubview:self.click];
            
            UILabel * label_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 2, CGRectGetMaxY(self.consume.frame) + 5, length_x, 20)];
            label_2.text = @"账户余额";
            label_2.font = [UIFont systemFontOfSize:14];
            label_2.textAlignment = NSTextAlignmentCenter;
            label_2.textColor = [UIColor grayColor];
            [cell addSubview:label_2];
            
            
            //4
            self.balance = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 3, 30, length_x + 3, 25)];
            self.balance.text = @"￥160.00";
            self.balance.font = [UIFont systemFontOfSize:15];
            self.balance.textColor = [UIColor blackColor];
            self.balance.textAlignment = NSTextAlignmentCenter;
            [cell addSubview:self.balance];
            
            UILabel * label_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 3, CGRectGetMaxY(self.consume.frame) + 5, length_x, 20)];
            label_3.text = @"充值总额";
            label_3.font = [UIFont systemFontOfSize:14];
            label_3.textAlignment = NSTextAlignmentCenter;
            label_3.textColor = [UIColor grayColor];
            [cell addSubview:label_3];
            
        }
            break;
        case 1:
        {
            cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
            
            UILabel * text = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x / 2 - 27.5, 10, SCREEN_WIDTH - 5 - length_x / 2 - 27.5 - 15, 20)];
            text.text = @"昨日访问来源分析";
            text.font = [UIFont systemFontOfSize:14];
            text.textColor = navi_bar_bg_color;
            [cell addSubview:text];
            
            //下面的数据分析
            UIImageView * view_bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 100)];
//            view_bg.image = [UIImage imageNamed:@"whith_bg.png"];
            view_bg.backgroundColor = [UIColor whiteColor];
            [cell addSubview:view_bg];
            
            //1
            UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(5 + length_x / 2 - 27.5, 50 - 27.5, 55, 55)];
            image.image = [UIImage imageNamed:@"fenxi@2x.png"];
            [view_bg addSubview:image];
            
            //2
            self.baidu_pc = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x, 30, length_x, 25)];
            self.baidu_pc.text = @"66";
            self.baidu_pc.font = [UIFont systemFontOfSize:15];
//            self.baidu_pc.backgroundColor = [UIColor orangeColor];
            self.baidu_pc.textAlignment = NSTextAlignmentCenter;
            [view_bg addSubview:self.baidu_pc];
            
            UILabel * label_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x, CGRectGetMaxY(self.baidu_pc.frame) + 5, length_x, 20)];
            label_1.text = @"电脑访问";
            label_1.font = [UIFont systemFontOfSize:14];
            label_1.textAlignment = NSTextAlignmentCenter;
            label_1.textColor = [UIColor grayColor];
            [view_bg addSubview:label_1];
            
            
            //3
            self.baidu_phone = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 2, 30, length_x, 25)];
            self.baidu_phone.text = @"29";
            self.baidu_phone.font = [UIFont systemFontOfSize:15];
//            self.baidu_phone.backgroundColor = [UIColor orangeColor];
            self.baidu_phone.textAlignment = NSTextAlignmentCenter;
            [view_bg addSubview:self.baidu_phone];
            
            UILabel * label_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 2, CGRectGetMaxY(self.baidu_pc.frame) + 5, length_x, 20)];
            label_2.text = @"手机访问";
            label_2.font = [UIFont systemFontOfSize:14];
            label_2.textAlignment = NSTextAlignmentCenter;
            label_2.textColor = [UIColor grayColor];
            [view_bg addSubview:label_2];
            
            
            //4
            self.interlinkage = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 3, 30, length_x, 25)];
            self.interlinkage.text = @"28";
            self.interlinkage.font = [UIFont systemFontOfSize:15];
//            self.interlinkage.backgroundColor = [UIColor orangeColor];
            self.interlinkage.textAlignment = NSTextAlignmentCenter;
            [view_bg addSubview:self.interlinkage];
            
            UILabel * label_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 3, CGRectGetMaxY(self.baidu_pc.frame) + 5, length_x, 20)];
            label_3.text = @"百度搜索";
            label_3.font = [UIFont systemFontOfSize:14];
            label_3.textAlignment = NSTextAlignmentCenter;
            label_3.textColor = [UIColor grayColor];
            [view_bg addSubview:label_3];
        }
            break;
        case 2:
        {
            cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
            
            self.keywords_sum = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x / 2 - 27.5, 10, SCREEN_WIDTH - 5 - length_x / 2 - 27.5 - 15, 20)];
            self.keywords_sum.text = @"昨日排名[关键数总数:11]";
            self.keywords_sum.font = [UIFont systemFontOfSize:14];
            self.keywords_sum.textColor = navi_bar_bg_color;
            [cell addSubview:self.keywords_sum];
            
            
            //下面的数据分析
            UIImageView * view_bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 100)];
//            view_bg.image = [UIImage imageNamed:@"whith_bg.png"];
            view_bg.backgroundColor = [UIColor whiteColor];
            [cell addSubview:view_bg];
            
            //1
            UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(5 + length_x / 2 - 27.5, 50 - 27.5, 55, 55)];
            image.image = [UIImage imageNamed:@"paiming@2x.png"];
            [view_bg addSubview:image];
            
            //2
            self.page_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x, 30, length_x, 25)];
            self.page_1.text = @"1";
            self.page_1.font = [UIFont systemFontOfSize:15];
//            self.page_1.backgroundColor = [UIColor orangeColor];
            self.page_1.textAlignment = NSTextAlignmentCenter;
            [view_bg addSubview:self.page_1];
            
            UILabel * label_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x, CGRectGetMaxY(self.page_1.frame) + 5, length_x, 20)];
            label_1.text = @"第一页";
            label_1.font = [UIFont systemFontOfSize:14];
            label_1.textAlignment = NSTextAlignmentCenter;
            label_1.textColor = [UIColor grayColor];
            [view_bg addSubview:label_1];
            
            
            //3
            self.page_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 2, 30, length_x, 25)];
            self.page_2.text = @"2";
            self.page_2.font = [UIFont systemFontOfSize:15];
//            self.page_2.backgroundColor = [UIColor orangeColor];
            self.page_2.textAlignment = NSTextAlignmentCenter;
            [view_bg addSubview:self.page_2];
            
            UILabel * label_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 2, CGRectGetMaxY(self.page_1.frame) + 5, length_x, 20)];
            label_2.text = @"第二页";
            label_2.font = [UIFont systemFontOfSize:14];
            label_2.textAlignment = NSTextAlignmentCenter;
            label_2.textColor = [UIColor grayColor];
            [view_bg addSubview:label_2];
            
            
            //4
            self.page_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 3, 30, length_x, 25)];
            self.page_3.text = @"8";
            self.page_3.font = [UIFont systemFontOfSize:15];
//            self.page_3.backgroundColor = [UIColor orangeColor];
            self.page_3.textAlignment = NSTextAlignmentCenter;
            [view_bg addSubview:self.page_3];
            
            UILabel * label_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 3, CGRectGetMaxY(self.page_1.frame) + 5, length_x, 20)];
            label_3.text = @"第三页";
            label_3.font = [UIFont systemFontOfSize:14];
            label_3.textAlignment = NSTextAlignmentCenter;
            label_3.textColor = [UIColor grayColor];
            [view_bg addSubview:label_3];

        }
            break;
        default:
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.row)
    {
        case 0:
        {
            XiaofeiViewController * xiaofeiViewController = [[XiaofeiViewController alloc] init];
            
            [self showViewController:xiaofeiViewController sender:nil];
        }
            break;
        case 1:
        {
            FangwenViewController * fangwenViewController = [[FangwenViewController alloc] init];
            
            [self showViewController:fangwenViewController sender:nil];
        }
            break;
        case 2:
        {
            PaimingViewController * paimingViewController = [[PaimingViewController alloc] init];
            
            [self showViewController:paimingViewController sender:nil];
        }
            break;
        default:
            break;
    }
}



#pragma mark - 默认登录数据的判断
- (void )p_loginMoren
{
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
    
    //如果上次处于登录状态，就核对账号密码
    if([[userdefault objectForKey:@"Login_Success"] isEqualToString:@"1"])
    {
        DataProvider * dataprovider = [[DataProvider alloc] init];
        
        [dataprovider setDelegateObject:self setBackFunctionName:@"login:"];
        
        [dataprovider loginWithUsername:[userdefault objectForKey:@"account"] password:[userdefault objectForKey:@"password"]];
    } 
}

#pragma mark - 接口部分
- (void)login:(id )dict
{
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];

    if([dict[@"status"] integerValue] == 1)
    {
        @try
        {
            
        }
        @catch (NSException *exception)
        {
            
        }
        @finally
        {
            //保存登陆的状态
            [userdefault setObject:@"1" forKey:@"Login_Success"];
            
            //保存用户信息（后期可能更多）
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"token"]] forKey:@"token"];
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"uid"]] forKey:@"uid"];
            //头像
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"avatar64"]] forKey:@"head_pic"];
            //站点数
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"servicenum"]] forKey:@"servicenum"];
            //用户名
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"username"]] forKey:@"username"];
            //余额
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"balance"]] forKey:@"balance"];
            //昵称
            [userdefault setObject:[NSString stringWithFormat:@"%@",dict[@"nickname"]] forKey:@"nickname"];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else
    {
        //保存登陆的状态
        [userdefault setObject:@"0" forKey:@"Login_Success"];
    }
}



@end
